﻿using AutoMapper;
using MediatR;
using RBAC.Domain.SmartHealthcare.Hosiptial;
using RBAC.ErrorCode;
using RBAC.Interstructrue.Base;
using SmartHealthcare.Read.Api.Application.Command.Hospitalbeds;
using SmartHealthcare.Read.Api.DTo;

namespace SmartHealthcare.Read.Api.Application.Handler.Hospitalbeds
{
    /// <summary>
    /// 获取指定病房的病床处理器
    /// </summary>
    public class GetBedsHandler : IRequestHandler<GetBedsCommand, APIResult<List<HospitalBedDto>>>
    {
        private readonly IBaseRepository<HospitalBedModel> hospitalBedRepository;
        private readonly IBaseRepository<HospitalRoomModel> hospitalRoomRepository;
        private readonly IMapper mapper;
        private readonly ILogger<GetBedsHandler> logger;

        public GetBedsHandler(
            IBaseRepository<HospitalBedModel> hospitalBedRepository,
            IBaseRepository<HospitalRoomModel> hospitalRoomRepository,
            IMapper mapper,
            ILogger<GetBedsHandler> logger)
        {
            this.hospitalBedRepository = hospitalBedRepository;
            this.hospitalRoomRepository = hospitalRoomRepository;
            this.mapper = mapper;
            this.logger = logger;
        }

        /// <summary>
        /// 获取指定病房的病床信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<APIResult<List<HospitalBedDto>>> Handle(GetBedsCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<HospitalBedDto>> res = new APIResult<List<HospitalBedDto>>();

            try
            {
                // 查询指定病房的所有未删除病床
                var query = hospitalBedRepository.GetALL()
                    .Where(x => x.IsDeleted == false && x.HospitalRoomId == request.HospitalRoomId);
                
                logger.LogInformation($"查询病房ID为{request.HospitalRoomId}的病床");

                var hospitalBeds = query.ToList();

                // 获取病房信息
                var hospitalRoom = hospitalRoomRepository.GetALL()
                    .FirstOrDefault(x => x.Id == request.HospitalRoomId && x.IsDeleted == false);

                if (hospitalRoom == null)
                {
                    res.Code = APIEnums.Fail;
                    res.Msg = "指定的病房不存在";
                    logger.LogWarning($"病房ID {request.HospitalRoomId} 不存在");
                    return Task.FromResult(res);
                }

                // 使用AutoMapper进行映射
                var hospitalBedDtos = mapper.Map<List<HospitalBedDto>>(hospitalBeds);

                // 填充病房号信息
                foreach (var dto in hospitalBedDtos)
                {
                    dto.HospitalRoomCode = hospitalRoom.HospitalRoomCode;
                }

                res.Data = hospitalBedDtos;
                res.Code = APIEnums.Success;
                res.Msg = "查询成功";
                logger.LogInformation($"查询病房{request.HospitalRoomId}的病床成功，共查询到{hospitalBedDtos.Count}张病床");
            }
            catch (Exception ex)
            {
                res.Code = APIEnums.Fail;
                res.Msg = "查询病床失败";
                logger.LogError(ex, "查询指定病房病床时发生异常");
            }

            return Task.FromResult(res);
        }
    }
}
